package com.android.tnovoselec.schedule.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.android.tnovoselec.schedule.R;
import com.android.tnovoselec.schedule.activity.ItemDetailsActivity;
import com.android.tnovoselec.schedule.activity.MainActivity;
import com.android.tnovoselec.schedule.activity.MainActivity.ItemAddedListener;
import com.android.tnovoselec.schedule.dto.ItemDto;
import com.android.tnovoselec.schedule.dto.ItemsResultHolder;

public class ScheduleDayFragment extends SherlockFragment implements ItemAddedListener, OnItemClickListener{

	private ListView list = null;
	private ArrayAdapter<String> adapter;
	private List<ItemDto> items;
	private int position;
	private List<String> s;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle extras = getArguments();
		if (extras != null) {
			position = extras.getInt("position");
		}
		items = ItemsResultHolder.getItemsByDay(position);
		s = new ArrayList<String>();
		if (items == null) return;
		for (ItemDto item : items) {
			s.add(item.getTitle());
		}
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.schedule_day_fragment, container, false);
		list = (ListView) v.findViewById(R.id.schedule_day_fragment_list);
		list.setOnItemClickListener(this);
		adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, s);
		list.setAdapter(adapter);
		return v;
	}

	@Override
	public void onItemAdded() {
		items = ItemsResultHolder.getItemsByDay(position);
		s = new ArrayList<String>();
		if (items == null) return;
		for (ItemDto item : items) {
			s.add(item.getTitle());
		}
		
		adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, s);
		list.setAdapter(adapter);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		((MainActivity)getActivity()).registerListener(this);
	}
	@Override
	public void onPause() {
		((MainActivity)getActivity()).unregisterListener(this);
		super.onPause();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		if (parent == list){
			Toast.makeText(getActivity(), "Item id: " + items.get(position).getItemId(),Toast.LENGTH_SHORT).show();
			Intent i = new Intent(getActivity(), ItemDetailsActivity.class);
			i.putExtra("itemID", items.get(position).getItemId());
			startActivity(i);
		}
		
	}

}
